<template>
  <div class="login box">
    <!-- 背景 -- 视频也可以背景图片 -->
    <video autoplay loop muted
           src="../../public/static/video/background.mp4" alt="背景视频"></video>
    <div class="login">
      <div class="login-title">
        <img src="../../public/static/image/Logotitle.1ba5466.png" alt="">
        <p>一朵云推动另一朵云,一个灵魂唤醒另一个灵魂</p>
      </div>
      <div class="login_box">
        <div class="title">
          <span :class="{ active: login_type === 0 }" @click="login_type=0">密码登陆</span>
          <span :class="{ active: login_type === 1 }" @click="login_type=1">验证码登陆</span>
        </div>
        <div class="inp" v-if="login_type==0">
          <input v-model="username" type="text" placeholder="用户名 / 手机号码" class="user">
          <input v-model="password" type="password" placeholder="密码" class="pwd">
          <!-- 占位 -->
          <div id="geetest1"></div>
          <div class="rember">
            <p>
              <input type="checkbox" class="no" name="a" v-model="remeber"/>
              <span>记住密码</span>
            </p>
            <p>忘记密码</p>
          </div>
          <button class="login_btn" @click="loginHeader">登陆</button>
          <p class="go_login" >没有账号? <router-link to="/user/reg">立即注册</router-link></p>
        </div>
        <div class="inp" v-show="login_type==1">
          <input v-model="username" type="text" placeholder="手机号码" class="user">
          <input v-model="password" type="text" placeholder="短信验证码" class="pwd">
          <button id="get_code">获取验证码</button>
          <button class="login_btn" @click="loginHeader">登陆</button>
          <p class="go_login">没有账号? <span>立即注册</span></p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>



export default {
  name: 'LoginComp',
  data() {
    return {
      // 登陆类型， 0.密码登陆， 1.短信登陆
      login_type: 0,
      username: "",
      password: "",
      remeber: false
    }
  },
  methods: {
    // 请求后端
    loginHeader() {
      if (this.login_type === 0) {
        // 发起请求，携带用户数据
        if (!this.username || !this.password) {
          this.$message.error('用户名和密码不能为空');
          return;
        }
        this.$axios.post(`${this.$settings.HOST}/user/api/login/`, {
          "username": this.username,
          'password': this.password
        }).then(res => {
          const data = res.data;

          // 保持用户的登陆状态
          if (this.remeber) {
            // 记住登陆，使用本地存储 保存到浏览器本地存储
            sessionStorage.clear();
            localStorage.setItem("access", data.access);
            localStorage.setItem("refresh", data.refresh);
            localStorage.setItem("id", data.id)
          } else {
            // 未点击记住登陆，使用浏览器存储 保存到浏览器存储
            localStorage.clear();
            sessionStorage.setItem("access", data.access);
            sessionStorage.setItem("refresh", data.refresh);
            sessionStorage.setItem("id", data.id)
          }
          this.$message.success('登陆成功')
          this.$router.push('/homePage');
        }).catch(error => {
          // 登陆失败弹窗
          this.$message.error("用户名或密码错误,请检查后登陆");
          console.log(error);
        })
      }
    }
  }
}
</script>

<style scoped>
.box {
  width: 100%;
  height: 100vh;
  position: relative;
  overflow: hidden;
}

.box video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.box .login {
  position: absolute;
  width: 400px;
  height: auto;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(255, 255, 255, 0.8);
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .5);
}

.login .login-title {
  width: 100%;
  text-align: center;
  margin-bottom: 20px;
}

.login-title img {
  width: 150px;
  height: auto;
}

.login-title p {
  font-family: PingFangSC-Regular;
  font-size: 16px;
  color: #333;
  letter-spacing: .29px;
  padding-top: 10px;
}

.login_box {
  width: 100%;
  height: auto;
}

.login_box .title {
  font-size: 18px;
  color: #9b9b9b;
  letter-spacing: .32px;
  border-bottom: 1px solid #e6e6e6;
  display: flex;
  justify-content: space-around;
  padding: 10px 0;
  margin-bottom: 20px;
  cursor: pointer;
}

.login_box .title span:nth-of-type(1) {
  color: #4a4a4a;
  border-bottom: 2px solid #84cc39;
}

.inp {
  width: 100%;
}

.inp input {
  border: 0;
  outline: 0;
  width: 100%;
  height: 40px;
  border-radius: 4px;
  border: 1px solid #d9d9d9;
  text-indent: 20px;
  font-size: 14px;
  background: #fff !important;
  margin-bottom: 15px;
}

.inp .rember {
  display: flex;
  justify-content: space-between; /* 两边对齐，忘记密码在右侧 */
  align-items: center; /* 垂直居中 */
  margin-top: 10px;
  font-size: 14px;
  color: #4a4a4a;
}

.inp .rember p:first-of-type {
  display: flex;
  align-items: center; /* 确保复选框和文字在一行居中对齐 */
}

.inp .rember input {
  width: 16px;
  height: 16px;
  margin-right: 5px; /* 调整复选框和文字间距 */
  cursor: pointer;
  border-radius: 4px; /* 圆角 */
  border: 1px solid #d9d9d9;
  background: #fff;
  transition: all 0.3s ease;
}

.inp .rember input:hover {
  border-color: #84cc39; /* 鼠标悬浮时边框高亮 */
}

.inp .rember span {
  font-size: 14px;
  color: #4a4a4a;
  transition: color 0.3s ease;
}

.inp .rember span:hover {
  color: #84cc39; /* 鼠标悬浮时文字颜色变化 */
}

.inp .rember p:nth-of-type(2) {
  color: #9b9b9b;
  font-size: 14px;
  cursor: pointer;
  margin-left: auto; /* 将“忘记密码”推到右侧 */
}

.inp .rember p:nth-of-type(2):hover {
  color: #4a4a4a; /* 鼠标悬浮时字体颜色变深 */
}


.login_btn {
  width: 100%;
  height: 40px;
  background: #84cc39;
  border-radius: 5px;
  font-size: 16px;
  color: #fff;
  margin-top: 20px;
}

.inp .go_login {
  text-align: center;
  font-size: 14px;
  color: #9b9b9b;
  margin-top: 15px;
}

.inp .go_login span {
  color: #84cc39;
  cursor: pointer;
}
</style>